package module.admin;
import bean.Admin;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;

/**
 * @Author leiHua
 * @Date 2022/5/25 22:16
 */
public class AdminSidebar extends JPanel {
    public static int height = AdminView.jframeHeight - AdminHead.height - 100;  //该容器的高
    public static int width = 300;       //该容器的宽
    JTree tree;  //树

    public AdminSidebar() {
        // tree-> root-> 结点（可以有子节点）
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("系统管理");
        tree = new JTree(root);
        tree.setRowHeight(50);
        tree.setFont(new Font("宋体",Font.TYPE1_FONT,30));
        tree.setForeground(new Color(47, 53, 66));
        tree.setSize(300, 400);
        DefaultMutableTreeNode competition = new DefaultMutableTreeNode("比赛管理");
        competition.add(new DefaultMutableTreeNode(new Name("报名审批")));
        competition.add(new DefaultMutableTreeNode(new Name("比赛管理")));
        root.add(competition);

        DefaultMutableTreeNode athlete = new DefaultMutableTreeNode("运动员管理");
        athlete.add(new DefaultMutableTreeNode("运动员信息"));
        athlete.add(new DefaultMutableTreeNode("导入运动员信息"));
        root.add(athlete);
        DefaultMutableTreeNode img = new DefaultMutableTreeNode("资源管理");
        img.add(new DefaultMutableTreeNode("轮播图图片管理"));
        img.add(new DefaultMutableTreeNode("更改首页URL链接"));
        root.add(img);

        this.add(tree);
        //监听器动作
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); //获取到选择的结点

                if (node == null)
                    return;
                if(node.getUserObject().toString().equals("系统管理")){
                    AdminContent.initJpanel.setVisible(false);
                    AdminContent.competitionPanel.setVisible(false);
                    AdminContent.checkJpanel.setVisible(false);
                    AdminContent.writePanel.setVisible(false);
                    AdminContent.athletePanel.setVisible(false);
                    AdminContent.updateUrlPanel.setVisible(false);
                }
                if (node.isLeaf()) {  //如果是叶子结点
                    //Name user = (Name) object;
                    String name= node.getUserObject().toString();  //获取叶子结点的名字
                    AdminContent.initJpanel.setVisible(false);
                    AdminContent.competitionPanel.setVisible(false);
                    AdminContent.checkJpanel.setVisible(false);
                    AdminContent.writePanel.setVisible(false);
                    AdminContent.athletePanel.setVisible(false);
                    AdminContent.updateUrlPanel.setVisible(false);
                    //点击到相对应的结点 ，显示相关的Jpanel
                    if(name.equals("报名审批")){
                        AdminContent.checkJpanel.setVisible(true);
                    }else if (name.equals("比赛管理")){
                        AdminContent.competitionPanel.setVisible(true);
                    }else if(name.equals("导入运动员信息")){
                        AdminContent.writePanel.setVisible(true);
                    }else if(name.equals("运动员信息")){
                        AdminContent.athletePanel.setVisible(true);
                    }else if(name.equals("更改首页URL链接")){
                        AdminContent.updateUrlPanel.setVisible(true);
                    }
                }
            }
        });
        this.setBounds(50, AdminHead.height, width, height);
    }

    class Name {
        private String name;
        public Name(String n) {
            name = n;
        }
        public String toString() {
            return name;
        }
    }
}
